package com.zaker.touchtv;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ContentFrameLayout;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import static com.zaker.touchtv.utils.Constants.LOG_TAG;

public class BaseActivity extends AppCompatActivity {

  private Toolbar mToolbar;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setupStatusBar();
  }

  @Override
  public void setContentView(@LayoutRes int layoutResID) {
    ViewGroup contentParent = (ViewGroup) findViewById(android.R.id.content);
    View layoutView = LayoutInflater.from(this).inflate(layoutResID, contentParent, false);
    setContentView(layoutView);
  }

  @Override
  public void setContentView(View view) {
    setContentView(view, view.getLayoutParams());
  }

  @Override
  public void setContentView(View view, @NonNull ViewGroup.LayoutParams params) {
    final int toolbarHeight = getResources().getDimensionPixelSize(R.dimen.toolbar_height);
    final int leftPadding = view.getPaddingLeft();
    final int topPadding = view.getPaddingLeft() + toolbarHeight;
    final int rightPadding = view.getPaddingLeft();
    final int bottomPadding = view.getPaddingLeft();
    view.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
    super.setContentView(view, params);
    setupToolbar();
  }

  /**
   * 是否支持基类工具栏，默认为true
   *
   * @return 为true则支持，反之不支持
   */
  protected boolean hasSupportBaseToolbar() {
    return true;
  }

  private void setupToolbar() {

    if (!hasSupportBaseToolbar()) {
      return;
    }

    ContentFrameLayout contentView = (ContentFrameLayout) findViewById(android.R.id.content);
    LayoutInflater.from(this).inflate(R.layout.layout_base_toolbar, contentView, true);
    mToolbar = (Toolbar) findViewById(R.id.base_toolbar);
    setSupportActionBar(mToolbar);

    mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Log.i(LOG_TAG, "mToolbar NavigationOnClickListener onClick is run!");
        finish();
      }
    });
    mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
      @Override
      public boolean onMenuItemClick(MenuItem item) {
        Log.i(LOG_TAG, "mToolbar OnMenuItemClickListener onMenuItemClick getItemId: "
            + item.getItemId());
        return true;
      }
    });
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
      actionBar.setDisplayHomeAsUpEnabled(true);
    }
  }

  private void setupStatusBar() {
    final View contentView = findViewById(android.R.id.content);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      contentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }
  }

  public void onUndevelopedFunctionEvent(View view) {
    final View contentView = findViewById(android.R.id.content);
    Snackbar.make(contentView, R.string.to_be_continue,
        Snackbar.LENGTH_SHORT).show();
  }

  void onlyForTest(View contentView) {

    inputViewInfo(contentView, " contentView R.id.content ");

    View decorView = getWindow().getDecorView();

    inputViewInfo(decorView, " decorView ");

    View rootView = decorView.getRootView();

    inputViewInfo(rootView, " rootView ");

    View homeView = findViewById(android.R.id.home);
    Log.i(LOG_TAG, " homeView 3 " + ((homeView == null) ? "is null" : " isn't null"));
    inputViewInfo(homeView, " homeView 3--- ");
  }

  private void inputViewInfo(View view, String tag) {

    if (view == null) {
      Log.i(LOG_TAG, tag + " is null ! ");
      return;
    }

    Log.i(LOG_TAG, tag + view.getClass().getName());

    if (!(view instanceof ViewGroup)) {
      return;
    }

    int count = ((ViewGroup) view).getChildCount();

    for (int i = 0; i < count; i++) {
      Log.i(LOG_TAG, tag + " i: " + i + " "
          + ((ViewGroup) view).getChildAt(i).getClass().getName());

      inputViewInfo(((ViewGroup) view).getChildAt(i), tag + " - " + i);
    }
  }

}
